package red.book._1._1;

/**
 * 二分查找的递归实现
 * @author wangbingwu
 *
 */
public class RecursiveImplementationOfBinarySearch {
	public static void main(String[] args) {
		int key = 5 ;
		int arrs[] = {0, 1, 2, 3, 4, 5, 6};
		
		System.out.println(binarySearch(key, arrs, 0, arrs.length - 1));
		
	}
	
	public static int binarySearch(int key, int [] arrs, int start, int end) {
		if (start > end) {
			return -1;
		}
		
		int mid = (start + end) / 2; // 或者 start + (end - start) /2 相当于开始位置加增量的部分得到中间的位置。
		
		if (arrs[mid] > key) {
			return binarySearch(key, arrs, start, mid -1);
		}else if (arrs[mid] < key) {
			return binarySearch(key, arrs, mid + 1, end);
		}else {
			return mid;
		}
	}
}
